
#include "FileUtils/TokenStream.h"
#include <iostream>
namespace utils
{
	TokenStream::TokenStream()
	{
		resetStream();
	}
	TokenStream::~TokenStream()
	{
	}

	bool TokenStream::isValidIdentifier(char c)
	{
		if((c>32)&&(c<127))
			
			return true;
		
		return false;
	}
	void TokenStream::resetStream()
	{
		startIndex = endIndex = 0;
	}

	void TokenStream::setTokenStream(const char*dat)
	{
		resetStream();
		
		data = dat;
	}

	bool TokenStream::getNextToken(std::string*buffer)
	{
		bool inString = false;
		
		startIndex = endIndex;
		
		int len = data.length()-1;
		
		if(startIndex >=len)
			return false;

		while((startIndex) < len && isValidIdentifier(data[startIndex]) == false)
		{
			startIndex++;
		}

		endIndex = startIndex + 1;
		
		if(data[startIndex] == '"')
		inString = !inString;

		if(startIndex < len)
		{
		while(endIndex < len &&(isValidIdentifier(data[endIndex]) ||inString == true))
		{
		
			if(data[endIndex] == '"')
		
			inString = !inString;
		
		endIndex++;
		}

		if(buffer!= 0)
		{
			int size = endIndex - startIndex;
			
			int index = startIndex;
			
			buffer->reserve(size+1);
			
			buffer->clear();

			for(int i = 0; i< size; i++)
			{
				buffer->push_back(data[index++]);
			}
		}
		return true;

		}
		return false;
	}

	bool TokenStream::getNextToken(std::string*token,std::string*buffer)
	{
		std::string tok;
		if(token == 0)
			return false;

		while(getNextToken(&tok))
		{
			if(( tok == *token) )
				return getNextToken(buffer);
		}
		return false;
	}
	bool TokenStream::moveToNextLine(std::string*buffer)
	{
		int length = data.length();

		if((startIndex < length) && (endIndex < length))
		{
		
			endIndex = startIndex;
		
		while((endIndex < length) &&(isValidIdentifier(data[endIndex]) || data[endIndex] == ' '))
		{
			endIndex++;
		
		}
		
		if(((endIndex - startIndex) == 0)||((endIndex - startIndex)> length))
		
			return false;
		
	
		
		if(buffer != NULL)
		{
		
		int size = (endIndex - startIndex);
		
		int index = startIndex;
		
		buffer->reserve(size+1 );
		
		buffer->clear();
		
		for(int i = 0; i < size; i++)
		{
		buffer->push_back(data[index++]);
		//index++;
		
		}
		
		}
		
		}
		
		else
		{
		return false;
		}
		
		endIndex++;
		
	    
		startIndex = endIndex + 1;
		
		return true;

	}
};